//
//  MBStoryAllListenCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStoryAllListenCell.h"

@interface MBStoryAllListenCell ()<UITableViewDataSource,UITableViewDelegate> {
    UIView          *_bgView;
    UITableView     *_tableView;
}

@end
@implementation MBStoryAllListenCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400*PERCEN)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    _tableView = [[UITableView alloc]initWithFrame:_bgView.bounds style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [_bgView addSubview:_tableView];
}

- (void)setMusilArr:(NSMutableArray *)musilArr
{
    if (!_musilArr) {
        _musilArr = [[NSMutableArray alloc]init];
    }
    [_musilArr removeAllObjects];
    [_musilArr addObjectsFromArray:musilArr];
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _musilArr.count*(ISIPAD?100*PERCEN:80*PERCEN));
    _tableView.frame = _bgView.bounds;
    if (_musilArr.count == 0) {
        _tableView.hidden = YES;
    } else {
        _tableView.hidden = NO;
    }
    [_tableView reloadData];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, _musilArr.count*(ISIPAD?100*PERCEN:80*PERCEN));
    _tableView.frame = _bgView.bounds;
    [_tableView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - 表
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _musilArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellID = @"story_allList_Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //标题
        UILabel *_nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(25*PERCEN, (80*PERCEN-20)/2., 200, 20)];
        _nickNameLb.backgroundColor = [UIColor clearColor];
        _nickNameLb.textAlignment = NSTextAlignmentLeft;
        _nickNameLb.textColor = UIColorFromHex(0x333333);
        _nickNameLb.font = [UIFont systemFontOfSize:13.f];
        _nickNameLb.tag = 500;
        [cell addSubview:_nickNameLb];
        
        //更多按钮
        UIButton * _moreBt = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreBt.frame = CGRectMake(SCREEN_WIDTH-100*PERCEN, (60*PERCEN)/2., 100*PERCEN, 20*PERCEN);
        _moreBt.backgroundColor = [UIColor clearColor];
        _moreBt.tag = 502;
        [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
        [_moreBt setImage:[UIImage imageNamed:@"more"] forState:UIControlStateHighlighted];
        [_moreBt addTarget:self action:@selector(onMoreToSelectAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:_moreBt];
        
        //线
        UIView *_lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 80*PERCEN-1, SCREEN_WIDTH, 1)];
        _lineView.backgroundColor = [UIColorFromHex(0x999999) colorWithAlphaComponent:.35];
        _lineView.tag = 501;
        [cell addSubview:_lineView];
    }
    
    UILabel *nickNameLb = (UILabel *)[cell viewWithTag:500];
    UIView *lineView = (UIView *)[cell viewWithTag:501];
    UIButton *moreBt = (UIButton *)[cell viewWithTag:502];
    
    /*
     {
     AudioName = "3.mp3";
     AudioPath = "/upload/2016-10-9/3.mp3";
     AudioStatus = 1;
     AuditionNum = 3;
     CreateTime = 0;
     ID = 3;
     LyricPath = "<null>";
     }
     */
    if (_musilArr.count > indexPath.row) {
        NSDictionary *dic = [_musilArr objectAtIndex:indexPath.row];
        NSString *name = [dic objectForKey:@"AudioName"];
        NSString *title = [[name componentsSeparatedByString:@"."]firstObject];
        nickNameLb.text = [NSString stringWithFormat:@"%d %@",(int)indexPath.row+1,title];
        if (_musilArr.count-1 == indexPath.row) {
            lineView.hidden = YES;
        }
    }
    
    if (ISIPAD) {
        nickNameLb.frame = CGRectMake(480*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), (100*PERCEN-20)/2., 200, 20);
        lineView.frame = CGRectMake(450*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 100*PERCEN-1, SCREEN_WIDTH-900*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 1);
        moreBt.frame = CGRectMake(lineView.right-100*PERCEN, (60*PERCEN)/2., 100*PERCEN, 20*PERCEN);
    } else {
        nickNameLb.frame = CGRectMake(25*PERCEN, (100*PERCEN-20)/2., 200, 20);
        lineView.frame = CGRectMake(0, 80*PERCEN-1, SCREEN_WIDTH, 1);
        moreBt.frame = CGRectMake(SCREEN_WIDTH-100*PERCEN, (60*PERCEN)/2., 100*PERCEN, 20*PERCEN);

    }
    
    return cell;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (ISIPAD?100*PERCEN:80*PERCEN);
}

//更多
- (void)onMoreToSelectAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlByMoreButtonWithValue:withView:)]) {
        UITableViewCell *cell = (UITableViewCell *)bt.superview;
        if ([cell isKindOfClass:[UITableViewCell class]]) {
            NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
            if (_musilArr.count > indexPath.row) {
                NSDictionary *dic = [_musilArr objectAtIndex:indexPath.row];
                [_delegate onSelectControlByMoreButtonWithValue:dic withView:self];
            }
        }
       
    }
}

//点击单个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlByCellWithValue:withView:)]) {
         NSDictionary *dic = [_musilArr objectAtIndex:indexPath.row];
        [_delegate onSelectControlByCellWithValue:dic withView:self];
    }
}

@end
